package Offer2018.java8;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.UnaryOperator;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 *
 */

//测试Stream的实例化
public class StreamAPITest {

    //创建Steam方式一:通过集合
    @Test
    public void test1() {
        List<Employee> employees = EmployeeData.getEmployees();
        //default Stream<E> stream():返回一个顺序流
        Stream<Employee> stream = employees.stream();
        //default Stream<E> parallelStream():返回一个并行流,并行操作。顺序不保证
        Stream<Employee> parallelStream = employees.parallelStream();
    }

    //创建Steam方式二:通过数组
    @Test
    public void test2() {
        int[] arr = {1, 2, 3, 4, 5, 6};
        //调用Arrays类的public static <T> Stream<T> stream(T[] array) 返回一个流
        IntStream stream = Arrays.stream(arr);

        Employee e1 = new Employee(1001, "Tom");
        Employee e2 = new Employee(1001, "Jerry");
        Employee[] arr1 = {e1, e2};
        Stream<Employee> stream1 = Arrays.stream(arr1);
    }


    //创建Stream方式三:通过Stream的of()
    @Test
    public void test3() {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
    }

    //创建 Stream方式四：创建无限流
    @Test
    public void test4() {

        //迭代
        //public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
        //遍历前10个偶数
        Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);

        //生成
        //public static<T> Stream<T> generate(Supplier<T> s)
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }
}
